package com.demo.maintenance.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.demo.maintenance.R;
import com.demo.maintenance.bean.QuizBean;
import com.demo.maintenance.contant.Global;
import com.demo.maintenance.utils.AntiShakeUtils;
import com.demo.maintenance.utils.HttpManager;
import com.demo.maintenance.utils.SystemBarTintManager;
import com.demo.maintenance.utils.TimeUtils;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;

import java.util.ArrayList;
import java.util.List;

public class QuizListActivity extends AppCompatActivity {

    private RecyclerView recycler_view;
    private ListAdapter listAdapter;
    private List<QuizBean.Data> datasource = new ArrayList<>();
    private FloatingActionButton btn_create;
    private long courseId = -1;
    private long childId = -1;

    @Override
    protected void onResume() {
        super.onResume();
        requestData();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        QMUIStatusBarHelper.translucent(this);
        SystemBarTintManager.setStatusBarDarkTheme(this, true);
        setContentView(R.layout.activity_list_quiz);

        findViewById(R.id.common_back_iv).setOnClickListener(v -> finish());
        ((TextView) findViewById(R.id.common_content_tv)).setText("Quiz List");

        btn_create = findViewById(R.id.btn_create);
        btn_create.setOnClickListener(v -> {
            if (AntiShakeUtils.isInvalidClick(v)) {
                return;
            }
            Intent intent = new Intent(QuizListActivity.this, QuizUploadActivity.class);
            intent.putExtra("courseId", courseId);
            intent.putExtra("childId", childId);
            startActivity(intent);
        });

        listAdapter = new ListAdapter();
        recycler_view = findViewById(R.id.recycler_view);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));
        recycler_view.setAdapter(listAdapter);

        if (getIntent() != null){
            courseId = getIntent().getLongExtra("courseId", -1);
            childId = getIntent().getLongExtra("childId", -1);
        }

        if (!"guardian".equals(Global.userResponse.data.role)){
            if (courseId == -1){
                btn_create.setVisibility(View.GONE);
            }else {
                btn_create.setVisibility(View.VISIBLE);
            }
        }else {
            btn_create.setVisibility(View.GONE);
        }
    }

    private void requestData() {

        WaitDialog.show("Waiting");

        String url = Global.ipAddress + "/system/quiz/listAll?phone=" + Global.userResponse.data.userPhone;
        if (courseId != -1){
            url += "&courseId=" + courseId;
        }
        if (childId != -1){
            url += "&childId=" + childId;
        }

        try {
            HttpManager.get(url, new HttpManager.NetworkCallBack() {
                @Override
                public void onSuccess(String successInfo) {
                    try {
                        Log.e("monitoring", successInfo);

                        QuizBean assignmentBean = JSON.parseObject(successInfo, QuizBean.class);
                        if (assignmentBean.code == 200) {
                            runOnUiThread(() -> {
                                if (assignmentBean.rows != null && !assignmentBean.rows.isEmpty()) {
                                    datasource.clear();
                                    datasource.addAll(assignmentBean.rows);
                                    listAdapter.notifyDataSetChanged();
                                }
                            });
                        } else {
                            Log.e("monitoring", "get list failed !");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(String errorInfo) {
                    Log.e("monitoring", errorInfo);
                }

                @Override
                public void onFinal() {
                    super.onFinal();
                    WaitDialog.dismiss();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            WaitDialog.dismiss();
        }
    }

    private class ListAdapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(QuizListActivity.this)
                    .inflate(R.layout.recycler_item_quiz, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            QuizBean.Data item = datasource.get(position);
            holder.tv_child_name.setText(item.childName);
            holder.tv_quiz_name.setText(item.quizName);
            holder.tv_point.setText(String.valueOf(item.quizPoint));
            holder.tv_quiz_desc.setText(item.quizDesc);
            holder.tv_course_name.setText(item.courseName);
            holder.tv_create_time.setText(TimeUtils.format(item.createTime));
        }

        @Override
        public int getItemCount() {
            return datasource.size();
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_quiz_name;
        private TextView tv_child_name;
        private TextView tv_point;
        private TextView tv_course_name;
        private TextView tv_create_time;
        private TextView tv_quiz_desc;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tv_quiz_name = itemView.findViewById(R.id.tv_quiz_name);
            tv_child_name = itemView.findViewById(R.id.tv_child_name);
            tv_point = itemView.findViewById(R.id.tv_point);
            tv_course_name = itemView.findViewById(R.id.tv_course_name);
            tv_create_time = itemView.findViewById(R.id.tv_create_time);
            tv_quiz_desc = itemView.findViewById(R.id.tv_quiz_desc);
        }
    }
}
